package com.whjk.safework.controller;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.whjk.safework.utils.LoaclDateUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping(value = "/dic")
public class Dic {




    @RequestMapping(value="/datestr",method = RequestMethod.GET)
    public String datestr(){

      /*  Date    today           =   DateUtil.date();
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        int     daynum          =   DateUtil.thisDayOfWeek(); //星期一是第二天。
        System.out.println(nextWeek);
        System.out.println(todaystr);
        System.out.println(todaydate.toString());
        Date newWeekday     =   DateUtil.offsetWeek(todaydate,1);
        Date newMonthDay    =   DateUtil.offsetMonth(todaydate,1);
        Date newYearDay     =   LoaclDateUtil.offsetYear(todaydate,1);
        return newWeekday.toString()+"_"+newMonthDay.toString()+"_"+newYearDay.toString();
       */


        Date    today       = DateUtil.date();
        int     quarter     = DateUtil.quarter(today);
        Date    begin       = DateUtil.beginOfQuarter(today);
        Date    end         = DateUtil.endOfQuarter(today);

        System.out.println("季初时间：" + begin);
        System.out.println("季末时间：" + end);

        Row weekstartday           =   LoaclDateUtil.getThisWeekStartDay(1);
        Row NextMonthStartDay      =   LoaclDateUtil.getThisMonthStartDay(1);
        Row NextQuarterStartDay    =   LoaclDateUtil.getThisQuarterStartDay(1);
        Row NextYearStartDay       =   LoaclDateUtil.getThisYearStartDay(1);

        Long  num =  Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and year=?",22,6,DateUtil.year(DateUtil.date()));
        System.out.println("num：" + num);
        return "周:"+weekstartday.toString()+"<br/>月："+NextMonthStartDay.toString()+"<br/>季度："+NextQuarterStartDay+"<br/>年："+NextYearStartDay;
    }

    @RequestMapping(value="/workerRecord",method = RequestMethod.GET)
    public Object workerRecord(){

        String                  positionSql      = "select DISTINCT(t_worker.position_id) as id from t_worker";
        List<Row>               positionRows     = Db.selectListBySql(positionSql);
        Map<Integer,List<Row>>  orgSafeTargetMap = new HashMap<Integer,List<Row>>();

        for (Row r:positionRows) {
            Integer     position_id     =   r.getInt("id");
            String      orgSafeSql      =   "select * from t_org_safetarget where duty_id=?";
            List<Row>   orgSafeRows     =   Db.selectListBySql(orgSafeSql,position_id);
            orgSafeTargetMap.put(position_id,orgSafeRows);
        }



        String      sql     = "select * from t_worker where work_status = ?";
        List<Row>   rows    = Db.selectListBySql(sql,0);

        for (Row r:rows) {
            Integer  workerId     =  Integer.parseInt(r.get("id").toString());
            Integer  positionId   =  Integer.parseInt(r.get("position_id").toString());

            List<Row>   orgSafeRows  = orgSafeTargetMap.get(positionId);
            List<Row>   in_row_list  = new ArrayList<>();

            for(Row orgSafeR:orgSafeRows) {
                Row insertRow = new Row();

                insertRow.set("year",DateUtil.year(DateUtil.date()));
                insertRow.set("worker_id",workerId);
                insertRow.set("target_id",orgSafeR.get("org_target_id"));
                insertRow.set("target_str",orgSafeR.get("org_target_str"));
                insertRow.set("dutyinfo_id",orgSafeR.get("org_dutyinfo_id"));
                insertRow.set("dutyinfo_str",orgSafeR.get("org_dutyinfo_str"));
                insertRow.set("plan_id",orgSafeR.get("org_target_id"));
                insertRow.set("plan_str",orgSafeR.get("org_target_str"));
                insertRow.set("check_id",orgSafeR.get("org_check_id"));
                insertRow.set("check_str",orgSafeR.get("org_check_str"));
                insertRow.set("check_pc",orgSafeR.get("check_pc"));
                insertRow.set("check_standard",orgSafeR.get("check_standard"));
                insertRow.set("kind_id",orgSafeR.get("kind_id"));
                insertRow.set("create_time", DateUtil.date());
                insertRow.set("safetarget_id", orgSafeR.get("id"));

                if(orgSafeR.get("check_pc").equals("1次/每年")){

                    long yearnum  =  Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and year=?",orgSafeR.get("id"),workerId,DateUtil.year(DateUtil.date()));
                    if(yearnum==0){
                        Row  yearRow  = LoaclDateUtil.getThisYearStartDay(1);
                        insertRow.set("start_time", yearRow.get("startDay"));
                        insertRow.set("end_time", yearRow.get("endDay"));
                        Db.insert("t_worker_target",insertRow);
                    }
                  //
                }


                if(orgSafeR.get("check_pc").equals("1次/每季度")){

                    Row  quarterRow  = LoaclDateUtil.getThisQuarterStartDay(1);
                    long quarternum  =  Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and start_time=? and end_time =?",orgSafeR.get("id"),workerId,quarterRow.get("startDay"),quarterRow.get("endDay"));


                    if(quarternum==0){
                        insertRow.set("start_time", quarterRow.get("startDay"));
                        insertRow.set("end_time", quarterRow.get("endDay"));
                        Db.insert("t_worker_target",insertRow);
                    }
                }
                if(orgSafeR.get("check_pc").equals("1次/每月")){

                    Row  monthRow  = LoaclDateUtil.getThisMonthStartDay(1);
                    long monthnum  =  Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and start_time=? and end_time =?",orgSafeR.get("id"),workerId,monthRow.get("startDay"),monthRow.get("endDay"));
                    if(monthnum==0){
                        insertRow.set("start_time", monthRow.get("startDay"));
                        insertRow.set("end_time", monthRow.get("endDay"));
                        Db.insert("t_worker_target",insertRow);
                    }
                }

                if(orgSafeR.get("check_pc").equals("1次/每周")){
                    Row  weekRow  = LoaclDateUtil.getThisWeekStartDay(1);
                    long weeknum  =  Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and start_time=? and end_time =?",orgSafeR.get("id"),workerId,weekRow.get("startDay"),weekRow.get("endDay"));
                    if(weeknum==0){
                        insertRow.set("start_time", weekRow.get("startDay"));
                        insertRow.set("end_time", weekRow.get("endDay"));
                        Db.insert("t_worker_target",insertRow);
                    }
                }
            }
          //  System.out.println(result.length);
        }

        return  orgSafeTargetMap;
    }


    @RequestMapping(value="/week",method = RequestMethod.GET)
    public String  week(){

        String                  positionSql      = "select DISTINCT(t_worker.position_id) as id from t_worker";
        List<Row> positionRows     = Db.selectListBySql(positionSql);
        Map<Integer,List<Row>> orgSafeTargetMap = new HashMap<Integer,List<Row>>();

        for (Row r:positionRows) {
            Integer     position_id     =   r.getInt("id");
            String      orgSafeSql      =   "select * from t_org_safetarget where duty_id=?";
            List<Row>   orgSafeRows     =   Db.selectListBySql(orgSafeSql,position_id);
            orgSafeTargetMap.put(position_id,orgSafeRows);
        }
        String      sql     =   "select * from t_worker where work_status = ?";
        List<Row>   rows    =   Db.selectListBySql(sql,0);

        for (Row r:rows) {
            Integer     workerId     =  Integer.parseInt(r.get("id").toString());
            Integer     positionId   =  Integer.parseInt(r.get("position_id").toString());
            List<Row>   orgSafeRows  =  orgSafeTargetMap.get(positionId);
            List<Row>   in_row_list  =  new ArrayList<>();
            for(Row orgSafeR:orgSafeRows) {
                Row     insertRow = new Row();
                insertRow.set("year",DateUtil.year(DateUtil.date()));
                insertRow.set("worker_id",workerId);
                insertRow.set("target_id",orgSafeR.get("org_target_id"));
                insertRow.set("target_str",orgSafeR.get("org_target_str"));
                insertRow.set("dutyinfo_id",orgSafeR.get("org_dutyinfo_id"));
                insertRow.set("dutyinfo_str",orgSafeR.get("org_dutyinfo_str"));
                insertRow.set("plan_id",orgSafeR.get("org_plan_id"));
                insertRow.set("plan_str",orgSafeR.get("org_plan_str"));
                insertRow.set("check_id",orgSafeR.get("org_check_id"));
                insertRow.set("check_str",orgSafeR.get("org_check_str"));
                insertRow.set("check_pc",orgSafeR.get("check_pc"));
                insertRow.set("check_standard",orgSafeR.get("check_standard"));
                insertRow.set("kind_id",orgSafeR.get("kind_id"));
                insertRow.set("create_time", DateUtil.date());
                insertRow.set("safetarget_id", orgSafeR.get("id"));
                Date    todaydate       =   DateUtil.parse("2023-08-18 00:00:01", "yyyy-MM-dd HH:mm:ss");
                String  formatstartDay  =   "";
                if (orgSafeR.get("check_pc").equals("1次/每周")) {
                    for(int i=0;i<=41;i++) {
                        Date    startDay            =   DateUtil.offsetDay(todaydate,i);
                                formatstartDay      =   DateUtil.format(startDay, "yyyy-MM-dd");
                        System.out.println(formatstartDay);
                        Row weekRow  = LoaclDateUtil.getThisWeekStartDay(1, formatstartDay);
                        long weeknum = Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and start_time=? and end_time =? and check_pc='1次/每周'", orgSafeR.get("id"), workerId, weekRow.get("startDay"), weekRow.get("endDay"));
                        if (weeknum == 0) {
                            insertRow.set("start_time", weekRow.get("startDay"));
                            insertRow.set("end_time", weekRow.get("endDay"));
                            insertRow.set("day", formatstartDay);
                            Db.insert("t_worker_target", insertRow);
                        }
                    }
                }

                if(orgSafeR.get("check_pc").equals("1次/每年")){
                    for(int i=0;i<=41;i++) {
                        Date    startDay = DateUtil.offsetDay(todaydate, i);
                                formatstartDay = DateUtil.format(startDay, "yyyy-MM-dd");
                        long yearnum = Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and year=?  and check_pc='1次/每年'", orgSafeR.get("id"), workerId, DateUtil.year(startDay));
                        if (yearnum == 0) {
                            Row yearRow = LoaclDateUtil.getThisYearStartDay(1);
                            insertRow.set("start_time", yearRow.get("startDay"));
                            insertRow.set("end_time", yearRow.get("endDay"));
                            Db.insert("t_worker_target", insertRow);
                        }
                    }
                }


                if(orgSafeR.get("check_pc").equals("1次/每季度")){
                    for(int i=0;i<=41;i++) {
                        Date startDay = DateUtil.offsetDay(todaydate, i);
                            formatstartDay = DateUtil.format(startDay, "yyyy-MM-dd");
                        Row quarterRow = LoaclDateUtil.getThisQuarterStartDay(1,formatstartDay);
                        long quarternum = Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and start_time=? and end_time =?  and check_pc='1次/每季度'", orgSafeR.get("id"), workerId, quarterRow.get("startDay"), quarterRow.get("endDay"));
                        if (quarternum == 0) {
                            insertRow.set("start_time", quarterRow.get("startDay"));
                            insertRow.set("end_time", quarterRow.get("endDay"));
                            Db.insert("t_worker_target", insertRow);
                        }
                    }
                }

                if(orgSafeR.get("check_pc").equals("1次/每月")){
                    for(int i=0;i<=41;i++) {
                        Date    startDay = DateUtil.offsetDay(todaydate, i);
                                formatstartDay = DateUtil.format(startDay, "yyyy-MM-dd");
                        Row monthRow = LoaclDateUtil.getThisMonthStartDay(1,formatstartDay);
                        long monthnum = Db.selectCount("select count(*) from t_worker_target where  safetarget_id=? and worker_id=? and start_time=? and end_time =? and check_pc='1次/每月' ", orgSafeR.get("id"), workerId, monthRow.get("startDay"), monthRow.get("endDay"));
                        if (monthnum == 0) {
                            insertRow.set("start_time", monthRow.get("startDay"));
                            insertRow.set("end_time", monthRow.get("endDay"));
                            Db.insert("t_worker_target", insertRow);
                        }
                    }
                }


            }
            //  System.out.println(result.length);
        }
        return  "";
    }


    
}
